package cn.codesea.housework_user.ui.manager;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.google.gson.Gson;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.codesea.housework_user.R;
import cn.codesea.housework_user.entity.User;
import cn.codesea.housework_user.ui.BaseActivity;
import cn.codesea.housework_user.ui.LoginActivity;
import cn.codesea.housework_user.ui.Tab1HomeFragment;
import cn.codesea.housework_user.ui.Tab2MineChildFragment;
import cn.codesea.housework_user.ui.Tab2MineOldFragment;
import cn.codesea.housework_user.utils.SPUtil;

public class ManagerMainActivity extends BaseActivity {

    @BindView(R.id.navigation_bar)
    BottomNavigationBar bottomNavigationBar;

    Fragment tabFragment1;
    Fragment tabFragment2;
    Fragment tabFragment3;
    Fragment tabFragment4;
    private FragmentManager fManager;

    private int lastSelectedPosition = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_manager);
        ButterKnife.bind(this);
        fManager = getSupportFragmentManager();
        init();
    }

    public void init(){
        initData();// 初始化数据
        initView();// 初始化界面
    }

    private void initView(){

        // 默认第一个界面
        setChioceItem(0);

        // 底部导航栏，添加项
        bottomNavigationBar
                .addItem(new BottomNavigationItem(R.drawable.tab_5_white_service, "活动管理").setActiveColorResource(R.color.primary))
                .addItem(new BottomNavigationItem(R.drawable.tab_4_white_service, "服务管理").setActiveColorResource(R.color.primary))
                .addItem(new BottomNavigationItem(R.drawable.tab_2_white, "订单管理").setActiveColorResource(R.color.primary))
                .addItem(new BottomNavigationItem(R.drawable.ic_mine, "个人中心").setActiveColorResource(R.color.primary))
                .setFirstSelectedPosition(lastSelectedPosition)
                .initialise();
        bottomNavigationBar.setAutoHideEnabled(false);// 取消自动隐藏

        // 添加事件
        bottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener(){
            @Override
            public void onTabSelected(int position) {
                setScrollableText(position);// 点击事件
            }

            @Override
            public void onTabUnselected(int position) {

            }

            @Override
            public void onTabReselected(int position) {

            }
        });

    }

    private void initData(){

    }

    /**
     * 底部导航栏点击事件处理
     * @param position
     */
    private void setScrollableText(int position) {
        switch (position) {
            case 0:
                setChioceItem(0);
                break;
            case 1:
                setChioceItem(1);
                break;
            case 2:
                setChioceItem(2);
                break;
            case 3:
                setChioceItem(3);
                break;
            default:
                setChioceItem(0);
                break;
        }
    }

    public void setChioceItem(int index)
    {
        //重置选项+隐藏所有Fragment
        FragmentTransaction transaction = fManager.beginTransaction();
        hideFragments(transaction);
        switch (index) {
            case 0:
                if (tabFragment1 == null) {
                    // 如果fg1为空，则创建一个并添加到界面上
                    tabFragment1 = new Tab1ActManagerFragment();
                    transaction.add(R.id.container, tabFragment1);
                } else {
                    // 如果MessageFragment不为空，则直接将它显示出来
                    transaction.show(tabFragment1);
                }
                break;
            case 1:
                if (tabFragment2 == null) {
                    // 如果fg1为空，则创建一个并添加到界面上
                    tabFragment2 = new Tab2ServiceManagerFragment();
                    transaction.add(R.id.container, tabFragment2);
                } else {
                    // 如果MessageFragment不为空，则直接将它显示出来
                    transaction.show(tabFragment2);
                }
                break;
            case 2:
                if (tabFragment3 == null) {
                    // 如果fg1为空，则创建一个并添加到界面上
                    tabFragment3 = new Tab4OrderManageFragment();
                    transaction.add(R.id.container, tabFragment3);
                } else {
                    transaction.show(tabFragment3);
                }
                break;
            case 3:
                if (tabFragment4 == null) {
                    // 如果fg1为空，则创建一个并添加到界面上
                    tabFragment4 = new Tab3ManagerMineFragment();
                    transaction.add(R.id.container, tabFragment4);
                } else {
                    transaction.show(tabFragment4);
                }
                break;
        }
        transaction.commit();
    }

    //隐藏所有的Fragment,避免fragment混乱
    public void hideFragments(FragmentTransaction transaction) {
        if (tabFragment1 != null) {
            transaction.hide(tabFragment1);
        }
        if (tabFragment2 != null) {
            transaction.hide(tabFragment2);
        }
        if (tabFragment3 != null) {
            transaction.hide(tabFragment3);
        }
        if (tabFragment4 != null) {
            transaction.hide(tabFragment4);
        }
    }

}
